/**
 * SigninController
 *
 * @description :: Server-side logic for managing Signins
 * @help        :: See http://sailsjs.org/#!/documentation/concepts/Controllers
 */

module.exports = {
	
	index : function(req, res){
		var owner = req.session.user.userid;
		var day = FormService.getDay();
		
		Signin.findOne({
			'owner' : owner,
			'or' : [
				{'signinTime': { '>=': day.current.toDate(), '<': day.next.toDate() }},
				{'signoutTime': { '>=': day.current.toDate(), '<': day.next.toDate() }},
			]
			
		}).then(function (result) {
			return res.view('mobile/signin', {
				signin : result
			});
		}).catch(function(err){
			sails.log.error(err);
			return res.serverError(err);
		});
	},
	
	signin : function(req, res){
		var owner = req.session.user.userid;
		var action = req.param("action");
		var sign = {};
		if(action == 'out'){
			sign.signoutTime = new Date();
		}else{
			sign.signinTime = new Date();
		}
		
		Signin.findOrCreate({'owner':owner}).then(function (result) {
			Signin.update(owner,sign).then(function (result) {
				return res.send({success : true});
			});
		}).catch(function(err){
			sails.log.error(err);
			return res.send({
				success : false,
				error : err
			});
		});
		
	},
	
};

